package com.addressbook.tests;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;

import com.addressbook.fw.ApplicationManager;
import com.addressbook.fw.ContactData;
import com.addressbook.fw.GroupData;

public class TestBase {

	protected ApplicationManager app = new ApplicationManager();

	public TestBase() {

	}

	@BeforeClass
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile","application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app.init(properties);
		app.navigateTo().MainPage();
	}

	/*
	 * @AfterSuite public void closeWebDriver() throws Exception {
	 * WebDriverSingleton.killWebDriver(); }
	 */

	@AfterClass
	public void tearDown() throws Exception {
		app.stop();
	}

/*	@DataProvider
	public Iterator<GroupData[]> randomValidGroupGenerator() {
		List<GroupData[]> groups = new ArrayList<GroupData[]>();

		for (int i = 0; i < 2; i++) {
			GroupData group = new GroupData()
					.withName(generateRandomString("name"))
					.withHeader(generateRandomString("header"))
					.withFooter(generateRandomString("footer"));
			groups.add(new GroupData[] { group });
		}
		return groups.iterator();
	}

	private String generateRandomString(String basis) {
		Random rnd = new Random();
		if (rnd.nextInt(5) == 0) {
			return "";
		} else {
			return basis + rnd.nextInt();
		}
	}

	@DataProvider
	public Iterator<ContactData[]> randomValidContactGenerator() {
		List<ContactData[]> contacts = new ArrayList<ContactData[]>();

		DateData date = new DateData();
		for (int i = 0; i < 2; i++) {
			date.setRandomDate();
			ContactData contact = new ContactData()
					.withName(generateRandomString("name"))
					.withLastName(generateRandomString("surname"))
					.withAddress(generateRandomString("address"))
					.withHome(generateRandomString(""))
					.withMobile(generateRandomString(""))
					.withWork(generateRandomString(""))
					.withEmail(generateRandomString("asd@asd"))
					.withEmail2(generateRandomString("qwe@qwe"))
					.withBirthDay(date.getDay())
					.withBirthMonth(date.getMonth())
					.withBirthYear(date.getYear())
					// .withGroup("group_name");
					.withAddress2(generateRandomString("address2_"))
					.withHome2(generateRandomString(""));
			contacts.add(new ContactData[] { contact });
		}
		return contacts.iterator();
	}*/

	public static List<Object[]> wrapGroupDataForDataProvider(
			List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[] { group });
		}
		return list;
	}
	
	public static List<Object[]> wrapContactsForDataProvider(
            List<ContactData> contacts) {
    List<Object[]> list = new ArrayList<Object[]>();
    for (ContactData contact : contacts) {
            list.add(new Object[] { contact });
    }
    return list;
}
}